package com.device.operating.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.device.operating.dao.DepartmentRepository;
import com.device.operating.dao.UgRepository;
import com.device.operating.dao.UserRepository;
import com.device.operating.entity.Department;
import com.device.operating.entity.User;
import com.device.operating.entity.Usergroup;
import com.device.operating.service.DepartmentService;
import com.device.operating.service.UgService;
import com.device.operating.service.UserService;

@Service("DepartmentService")
@Transactional(propagation=Propagation.REQUIRED)
public class DepartmentServiceImpl implements DepartmentService{
	
	@Resource(name="userService")
	private UserService userService;
	
	@Resource(name="UgService")
	private UgService UgService;
	
	@Autowired
	private DepartmentRepository DepartmentRepository;

	@Override
	public void adddepartment(Department dp) {
		// TODO Auto-generated method stub
		DepartmentRepository.save(dp);
	}

	@Override
	public List<Department> querybyparentId(String parentId) {
		// TODO Auto-generated method stub
		return DepartmentRepository.findByParentId(parentId);
	}

	@Override
	public Department queryone(String dpId) {
		// TODO Auto-generated method stub
		return DepartmentRepository.findOne(dpId);
	}

	@Override
	public List<Department> quertbyname(String dpname) {
		// TODO Auto-generated method stub
		return DepartmentRepository.findByDpname(dpname);
	}

	@Override
	public List<Department> querybynameandparentId(String dpname,
			String parentId) {
		// TODO Auto-generated method stub
		return DepartmentRepository.findByDpnameAndParentId(dpname, parentId);
	}

	@Override
	public Department querydpls(String departmentId) {
		// TODO Auto-generated method stub
		while(true){
			Department dp=DepartmentRepository.findOne(departmentId);
			//dp=this.getparent(departmentId);
			if(dp.getParentId().equals("root")||dp.getParentId().equals("0"))
				return dp;
			departmentId=dp.getParentId();
		}
	}
	
	public Department getparent(String departmentId){
		return DepartmentRepository.findOne(DepartmentRepository.findOne(departmentId).getParentId());
	}
	
	@Override
	public List<Department> querydprela(String departmentId) {
		// TODO Auto-generated method stub
		List<Department> list=new ArrayList<>();
		List<String> listdpId=new ArrayList<>();
		listdpId.add(departmentId);
		while(true){
			List<String> liststr=new ArrayList<>();
			for(String dpId:listdpId){
				List<Department> ld=DepartmentRepository.findByParentId(dpId);
				list.addAll(ld);
				for(Department depart:ld){
					liststr.add(depart.getDepartmentId());
				}
			}
			listdpId.clear();
			listdpId.addAll(liststr);
			if(listdpId.size()==0){
				break;
			}
		}
		return list;
	}

	@Override
	public void deletedepartment(String dpId) {
		// TODO Auto-generated method stub
		DepartmentRepository.delete(dpId);
	}

	@Override
	public Map removedepartment(String departmentId) {
		// TODO Auto-generated method stub
		Map map=new HashMap<>();
		List<Department> list=querydprela(departmentId);
		if(list.size()>0){
			map.put("msg", "该部门有子部门，故无法删除");
			return map;
		}
		Map muser=userService.queryalluserbydepartmentId(departmentId, 0, 1,false);
		if(Integer.valueOf(muser.get("total").toString())>0){
			map.put("msg", "该部门或者子部门下有人员，故无法删除");
			return map;
		}
		Department dp=DepartmentRepository.findOne(departmentId);
		if(dp==null){
			map.put("msg", "错误！");
			return map;
		}
		map.put("parentId",dp.getParentId());
		DepartmentRepository.delete(departmentId);
		map.put("msg", "删除成功!");
		return map;
	}

	@Override
	public Map changedpname(Department dp) {
		Map map=new HashMap<>();
		String dpId=dp.getDepartmentId();
		// TODO Auto-generated method stub
		List<User> listu=userService.querybydepartmentId(dpId);
		for(User user:listu){//改变该部门下用户的名称
			user.setDpname(dp.getDpname());
			userService.addUser(user);
		}
		DepartmentRepository.save(dp);
		if(!dp.getParentId().equals("root")){//不是一级部门,直接返回
			map.put("msg", "修改成功!");
			return map;
		}
		List<Department> listchild=DepartmentRepository.findByRootdpId(dpId);//隶属部门为该部门
		for(Department depart:listchild){
			depart.setRootdpname(dp.getDpname());
			DepartmentRepository.save(depart);
		}
		List<User> listcuser=userService.queryByRootdpId(dpId);
		for(User user:listcuser){
			user.setRootdpname(dp.getDpname());
			userService.addUser(user);
		}
		List<Usergroup> listug=UgService.querybydpId(dpId);
		for(Usergroup ug:listug){
			ug.setDpname(dp.getDpname());
			UgService.addug(ug);
		}
		List<Usergroup> listrug=UgService.querybyrootdpId(dpId);
		for(Usergroup ug:listrug){
			ug.setRootdpname(dp.getDpname());
			UgService.addug(ug);
		}
		
		map.put("msg", "修改成功!");
		return map;
	}

	@Override
	public List<Department> queryall() {
		// TODO Auto-generated method stub
		List<Department> list=new ArrayList<>();
		Iterable<Department> iter=DepartmentRepository.findAll();
		Iterator<Department> it=iter.iterator();
		while(it.hasNext()){
			list.add(it.next());
		}
		return list;
	}
	
	
	
}
